﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace Interview_Prov
{
    public class Global : System.Web.HttpApplication
    {
        public static string UserName { get; set; }
        public static List<string> UserRoles { get; set; }
        public static string UserLevel { get; set; }
        public static bool IsAuthenticated { get; set; }
        public static int UserID { get; set; }

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            HttpApplication appl = (HttpApplication)sender;
            if (appl.Request.IsAuthenticated && appl.User.Identity is FormsIdentity)
            {
                FormsIdentity identity = (FormsIdentity)appl.User.Identity;
                UserName = identity.Name;
                var bll = new Business.BusinessLogic();
                UserRoles = bll.GetUserRoles(identity.Name);
                UserID = bll.GetUserID(identity.Name);
                IsAuthenticated = true;
                if (UserRoles.Contains("admin"))
                {
                    UserLevel = "admin";
                    return;
                }
                if (UserRoles.Contains("moderator"))
                {
                    UserLevel = "moderator";
                    return;
                }
                if (UserRoles.Contains("user"))
                {
                    UserLevel = "user";
                    return;
                }
            }
            else
            {
                UserLevel = "guest";
                IsAuthenticated = false;
                UserName = "";
                UserID = -1;
            }

        }

        protected void Application_Error(object sender, EventArgs e)
        {
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}